package com.yl.applicationsecurity.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.pojo.Result;
import com.yl.applicationsecurity.pojo.SgmSafeRisk;
import com.yl.applicationsecurity.pojo.SgmSafeRiskInsert;
import com.yl.applicationsecurity.pojo.SgmSafeRiskItem;
import com.yl.applicationsecurity.service.ScriptStateService;
import com.yl.applicationsecurity.service.SgmSafeRiskItemService;
import com.yl.applicationsecurity.service.SgmSafeRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Base64;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security   逻辑探针-安全漏洞实体类
 * @description:
 * @date 2025-02-08 11:58:32
 */
@RestController
@RequestMapping("/sgmSafeRisk")
public class SgmSafeRiskController {

    @Autowired
    private SgmSafeRiskService sgmSafeRiskService;

    @Autowired
    private SgmSafeRiskItemService sgmSafeRiskItemService;


    @Autowired
    private ScriptStateService scriptStateService;

    @PostMapping("/list")
    public Result list(String[] nationSelect, String[] systemNameSelect,Integer[] excessiveTypeArray,Integer[] statusArray,SgmSafeRisk sgmSafeRisk, @RequestParam(required = false) Date[] startAndEndDate, @RequestParam(defaultValue = "1", value = "pageNum")Integer pageNum
            , @RequestParam(defaultValue = "10", value = "pageSize")Integer pageSize ){
        PageInfo<SgmSafeRisk> sgmSafeRiskList = null;
        if(startAndEndDate!=null&&startAndEndDate.length!=2){
            sgmSafeRiskList =  sgmSafeRiskService.findListBySgmSafeRisk(nationSelect,systemNameSelect,sgmSafeRisk,null,null,pageNum,pageSize,excessiveTypeArray,statusArray);
        }else{
            sgmSafeRiskList =  sgmSafeRiskService.findListBySgmSafeRisk(nationSelect,systemNameSelect,sgmSafeRisk,startAndEndDate[0],startAndEndDate[1],pageNum,pageSize,excessiveTypeArray,statusArray);
        }

        return Result.success(sgmSafeRiskList);
    }

    @GetMapping ("/getRequestData")
    public Result getRequestData(Long safeRiskId){
        //根据告警ID查询出最近一次告警
        SgmSafeRiskItem sgmSafeRiskItem =  sgmSafeRiskItemService.findSafeRiskItemBySgmSafeRisk(safeRiskId);

        return Result.success(sgmSafeRiskItem);
    }



    @GetMapping ("/getDetails")
    public Result getDetails(Long safeRiskId){
        //根据传入的告警ID查询出最近一次告警
        SgmSafeRisk sgmSafeRisk = sgmSafeRiskService.findOneById(safeRiskId);

        return Result.success(sgmSafeRisk);
    }


    @GetMapping ("/getRiskItemList")
    public Result getRiskItemList(Long safeRiskId){
        //根据传入的告警ID查询出最近一次告警
        List<SgmSafeRiskItem> list  = sgmSafeRiskItemService.findListBySafeRiskId(safeRiskId);

        return Result.success(list);
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(HttpServletRequest request, String[]  ids, Integer updateStatus) {

        if (sgmSafeRiskService.updateStatus(ids, updateStatus) != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }



//    @RequestMapping("/insert")
    public Result insert(@RequestBody List<SgmSafeRiskInsert> list) {

        scriptStateService.updateDate("交互式逻辑探针-越权");
        //  时间转换String dateTimeStr = "2025-02-19 14:30:00";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        for (SgmSafeRiskInsert sgmSafeRiskInsert : list) {
            if(StringUtils.isEmpty(sgmSafeRiskInsert.getUrl())||StringUtils.isEmpty(sgmSafeRiskInsert.getSystemName())||StringUtils.isEmpty(sgmSafeRiskInsert.getDateTimeStr())||StringUtils.isEmpty(sgmSafeRiskInsert.getRequest())||StringUtils.isEmpty(sgmSafeRiskInsert.getResponse())||sgmSafeRiskInsert.getExcessiveType()==null){
                continue;
            }

            try {
                date = formatter.parse(sgmSafeRiskInsert.getDateTimeStr());

            } catch (ParseException e) {
                e.printStackTrace();
            }
            // request base64解码
            byte[] decodedBytes = Base64.getDecoder().decode(sgmSafeRiskInsert.getRequest());
            sgmSafeRiskInsert.setRequest( new String(decodedBytes));

            // response base64解码
            decodedBytes = Base64.getDecoder().decode(sgmSafeRiskInsert.getResponse());
            sgmSafeRiskInsert.setResponse(new String(decodedBytes));
            sgmSafeRiskService.add( sgmSafeRiskInsert.getUrl(), sgmSafeRiskInsert.getSystemName(), sgmSafeRiskInsert.getExcessiveType(), sgmSafeRiskInsert.getLevel(), sgmSafeRiskInsert.getCountry(), sgmSafeRiskInsert.getRequest() , sgmSafeRiskInsert.getResponse(),date,sgmSafeRiskInsert.getMenu(),sgmSafeRiskInsert.getAiCheck());

        }

        return Result.success("添加成功");

    }



}

